﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFExtended.Twitter.Activities.Designers;

namespace WFExtended.Twitter.Activities
{
    /// <summary>
    /// Returns the locations that Twitter has trending topic information for.
    /// </summary>
    [ToolboxBitmapAttribute(typeof(EntryPoint), "Resources.TrendToolbox.bmp")]
    [Designer(typeof(GetTrendLocationsDesigner))]
    [Description("Returns the locations that Twitter has trending topic information for.")]
    public class GetTrendLocations : TwitterOperationInvoker<List<TrendLocation>>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GetTrendLocations"/> class.
        /// </summary>
        public GetTrendLocations()
            : base("GetTrendLocations")
        {
            Latitude = new InArgument<string>();
            Longitude = new InArgument<string>();
        }


        /// <summary>
        /// The latitude to search around.
        /// </summary>
        /// <value>
        /// The latitude.
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("The latitude to search around.")]
        public InArgument<string> Latitude { get; set; }

        /// <summary>
        /// The longitude to search around. 
        /// </summary>
        /// <value>
        /// The longitude.
        /// </value>
        [Browsable(true)]
        [Category("Parameters")]
        [Description("The longitude to search around. ")]
        public InArgument<string> Longitude { get; set; }

        /// <summary>
        /// Caches the metadata.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            if (Latitude != null
                && Longitude != null)
            {
                if (Latitude.Expression != null
                    && Longitude.Expression != null)
                {
                    OperationName = "GetTrendLocationsWithOptions";
                }
                else
                    OperationName = "GetTrendLocations";
            }
     

            RuntimeArgument latitudeArgument = new RuntimeArgument("Latitude", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(Latitude, latitudeArgument);
            metadata.AddArgument(latitudeArgument);

            RuntimeArgument longitudeArgument = new RuntimeArgument("Longitude", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(Longitude, longitudeArgument);
            metadata.AddArgument(latitudeArgument);

            base.CacheMetadata(metadata);
        }

        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void Execute(NativeActivityContext context)
        {
            Parameters.Clear();

            if (Latitude.Expression != null
                && Longitude.Expression != null)
            {
                Parameters.Add("latitude", Latitude);
                Parameters.Add("longitude", Longitude);
            }

            base.Execute(context);
        }
    }
}
